package com.suishoupai.xmb.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.suishoupai.xmb.entity.User;
import com.suishoupai.xmb.exception.XmbException;
import com.suishoupai.xmb.utils.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by caojian on 2017/4/27.
 */
@Service
public class HuanxinService {
    @Autowired
    private HttpService httpService;
    @Value("${huanxin.host}")
    private String host;
    @Value("${huanxin.org_name}")
    private String orgName;
    @Value("${huanxin.app_name}")
    private String appName;
    @Value("${huanxin.client_id}")
    private String clientId;
    @Value("${huanxin.client_secret}")
    private String clientSecret;

    //获取token
    private String getToken() throws XmbException {
        String url = host + "/" + orgName + "/" + appName + "/token";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("client_id", clientId);
        jsonObject.put("client_secret", clientSecret);
        jsonObject.put("grant_type", "client_credentials");
        String json = jsonObject.toJSONString();
        String resultJson = httpService.post(url, json);
        JSONObject resultObject = JSON.parseObject(resultJson);
        String token = resultObject.getString("access_token");
        return token;
    }

    /**
     * 注册用户
     *
     * @param username
     * @param password
     * @throws XmbException
     */
    public void registeredUser(String username, String password) throws XmbException {
        String token = this.getToken();
        String url = host + "/" + orgName + "/" + appName + "/users";
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", username);
        jsonObject.put("password", password);
        jsonArray.add(jsonObject);
        String json = jsonArray.toJSONString();
        Map header = new HashMap();
        header.put("Authorization", "Bearer " + token);
        httpService.post(url, json, header);
    }

    /**
     * 发送消息
     *
     * @param username
     */
    public void sendMessage(String username, String message) throws XmbException {
        String token = this.getToken();
        String url = host + "/" + orgName + "/" + appName + "/messages";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("target_type", "users");
        JSONArray target = new JSONArray();
        target.add(username);
        JSONObject msg = new JSONObject();
        msg.put("type", "txt");
        msg.put("msg", message);
        jsonObject.put("target", target);
        jsonObject.put("msg", msg);
        String json = jsonObject.toJSONString();
        Map header = new HashMap();
        header.put("Authorization", "Bearer " + token);
        httpService.post(url, json, header);
    }
}
